from fastapi import APIRouter, HTTPException
from typing import List, Optional

from app.expertise.models import Expertise
from app.expertise.schemas import ExpertiseCreate, ExpertiseUpdate
from app.expertise.crud import expertise


expertise_router = APIRouter()

@expertise_router.get("/{expertise_id}", response_model=Optional[Expertise])
async def get_expertise(expertise_id : int) -> Optional[Expertise]:
    get_expertise = await expertise.get(id=expertise_id)
    if not get_expertise:
        raise HTTPException(
            status_code=404,
            detail="The expertise with this id does not exist in the system",
        )
    return get_expertise

@expertise_router.get("/", response_model=List[Expertise])
async def get_expertises() -> List[Expertise]:
    expertises = await expertise.get_multy()
    return expertises

@expertise_router.post("/create", response_model=Expertise)
async def create_expertise(expertise_schema: ExpertiseCreate) -> Optional[Expertise]:
    new_expertise = await expertise.create(schema=expertise_schema)
    if not new_expertise:
        raise HTTPException(
            status_code=404,
            detail="The expert or customer with this id does not exist in the system",
        )
    return new_expertise

@expertise_router.put("/update/{expertise_id}", response_model=Expertise)
async def update_expertise(expertise_id: int, expertise_schema: ExpertiseUpdate) -> Optional[Expertise]:
    updating_expertise = await expertise.update(id=expertise_id, schema=expertise_schema)
    if not updating_expertise:
        raise HTTPException(
            status_code=404,
            detail="The expertise with this id does not exist in the system",
        )
    return updating_expertise

@expertise_router.delete("/delete/{expertise_id}", response_model=Expertise)
async def delete_expertise(expertise_id: int) -> Optional[Expertise]:
    deleting_expertise = await expertise.delete(id=expertise_id)
    if not deleting_expertise:
        raise HTTPException(
            status_code=404,
            detail="The expertise with id does not exist in the system"
        ) 
    return deleting_expertise
